import {
    generateRandomId,
    InputType,
    StrategyKey,
    generateClassToId,
    getDomList,
    getDom,
    addErrorClass
} from '@/utils';
import type { BaseTableConfig } from '@/components/base-ui/base-table/type';
import type { StrategyParams } from '@/utils';

/**
 * 设备编码校验
 */
function getRuleFn(key: string, id: string) {
    return (params: StrategyParams) => {
        if (params.value) {
            const tableBodyEl = getDom('.el-table__body', params.tableInstance.$el);
            const tableRowEl = getDomList('.el-table__row', tableBodyEl!)?.[params.row!];
            const filterData = (params.value as Array<any>).filter((item: any) => !item[key])[0];

            if (filterData) {
                const validErrIndex = (params.value as Array<any>).findIndex(
                    (item: anyObj) => !item[key]
                );

                // 获取校验失败dom元素
                const validErrorEL = getDomList(
                    `.${generateClassToId(id)}`,
                    tableRowEl as HTMLElement
                )?.[validErrIndex];

                addErrorClass(validErrorEL!);
                return '请完善必填信息';
            }
        }
    };
}

export const tableConfig: BaseTableConfig = {
    items: [
        {
            modelKey: 'processCode',
            elInputProps: {
                showWordLimit: true,
                maxlength: '50'
            },
            columnProps: {
                label: '工序编码',
                width: 240
            },
            validProps: {
                id: generateRandomId(),
                required: true
            },
            validFormatType: InputType.NUMERIC_ALPHABET_SYMBOLS
        },
        {
            modelKey: 'processName',
            columnProps: {
                label: '工序名称',
                width: 240
            },
            elInputProps: {
                showWordLimit: true,
                maxlength: '50'
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            modelKey: 'remarks',
            elInputProps: {
                maxlength: '50',
                type: 'textarea',
                showWordLimit: true
            },
            columnProps: {
                label: '工序描述',
                width: 260
            },
            validProps: {
                id: generateRandomId()
            }
        },
        {
            modelKey: 'workingProcedureEquipments',
            slotName: 'workingProcedureEquipments',
            noEdit: true,
            columnProps: {
                label: '设备编码',
                width: 240,
                cellClassName: 'custom-cell'
            },
            validProps: {
                id: generateRandomId(),
                required: true
            },
            validRules: [
                {
                    validType: StrategyKey.IS_CUSTOMIZE,
                    ruleFn: getRuleFn('equipmentNumber', '1'),
                    errMsgFn: (row: number) => {
                        return `请完善工序序号为 ${row} 的设备编码信息`;
                    }
                }
            ]
        },
        {
            modelKey: 'workingProcedureEquipments',
            slotName: 'workingProcedureEquipmentsName',
            noEdit: true,
            columnProps: {
                label: '设备名称',
                width: 220,
                cellClassName: 'custom-cell'
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            modelKey: 'workingProcedureAuxiliary',
            slotName: 'workingProcedureAuxiliary',
            noEdit: true,
            columnProps: {
                label: '辅助资源编码',
                width: 240,
                cellClassName: 'custom-cell'
            },
            validProps: {
                id: generateRandomId()
            }
        },
        {
            modelKey: 'workingProcedureAuxiliaryName',
            slotName: 'workingProcedureAuxiliaryName',
            noEdit: true,
            columnProps: {
                label: '辅助资源名称',
                width: 220,
                cellClassName: 'custom-cell'
            },
            validProps: {
                id: generateRandomId()
            }
        }
    ],
    unAction: true
};
